using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

public class NetcodeStarter : NetworkBehaviour
{
    [SerializeField] private Button hostButton;
    [SerializeField] private Button clientButton;
    
    [SerializeField] private TextMeshProUGUI playerTypeText;
    [SerializeField] private TextMeshProUGUI playerNumText;
    
    private NetworkVariable<int> playersNum = new NetworkVariable<int>(0, NetworkVariableReadPermission.Everyone);
    
    // Start is called before the first frame update
    void Awake()
    {
        hostButton.onClick.AddListener(OnHostButtonClick);
        clientButton.onClick.AddListener(OnClientButtonClick);
    }

    private void Update()
    {
        playerNumText.text = $"players: {playersNum.Value.ToString()}";
        playerTypeText.text = IsServer ? "host" : "client";
        if (!IsServer) return;
        playersNum.Value = NetworkManager.Singleton.ConnectedClients.Count;
    }

    public void OnHostButtonClick()
    {
        NetworkManager.Singleton.StartHost();
    }
    
    public void OnClientButtonClick()
    {
        NetworkManager.Singleton.StartClient();
    }
    
}
